(function (vc) {

    vc.extends({
        data: {
            productDetailInfo: {
                product: {}
            },
            goodsNum: 1,
            productAppraiseInfo: [],
            productLabelList: []
        },
        _initMethod: function () {
            $that._queryProductByProductId(vc.getParam("productId"));
            $that._listShopAppraise(vc.getParam("productId"));

            $(".slide-detail .hdd li").each(function () {
                $(this).click(function () {
                    $(this).addClass("on").siblings().removeClass("on");
                    if ($(this).index() == 0) {
                        $(".box1detail,.otherbox,.evalute,.box1 h3.title").show();
                    } else if ($(this).index() == 1) {
                        $(".box1detail,.otherbox").hide();
                        $(".evalute").show();
                        $(".box1 h3.title").hide();
                    } else {
                        $(".box1detail,.evalute").hide();
                        $(".otherbox").show();
                        $(".box1 h3.title").hide();
                    }
                });
            });
            $that.websiteAccessLog();
        },
        _initEvent: function () {

        },
        methods: {
            _queryProductByProductId: function (_productId) {
                let param = {
                    params: {
                        page: -1,
                        row: 100,
                        productId: _productId
                    }
                }
                vc.http.apiGet(
                    'product.queryProduct',
                    param,
                    function (json, res) {
                        let _productDetailInfo = JSON.parse(json);
                        let product = _productDetailInfo.data[0];
                        let _productSpecValues = product.productSpecValues;
                        if (_productSpecValues != "undefined" && _productSpecValues != null && _productSpecValues != '') {
                            _productSpecValues.forEach(item => {
                                let _productSpecDetails = item.productSpecDetails;
                                let _specValue = '';
                                if (_productSpecDetails != null && _productSpecDetails != 'undefined' && _productSpecDetails != '') {
                                    _productSpecDetails.forEach(detail => {
                                        _specValue += (detail.detailValue + "/");
                                    });
                                }
                                item.specValue = _specValue;
                            });
                        }
                        $that.productDetailInfo.product = product;
                        $that._listProductLabel();
                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');
                    }
                );
            },
            _listShopAppraise: function (_productId) {
                let param = {
                    params: {
                        page: -1,
                        row: 100,
                        objId: _productId,
                        objType: '1001'
                    }
                }
                vc.http.apiGet(
                    'shopAppraise.listShopAppraise',
                    param,
                    function (json, res) {
                        $that.productAppraiseInfo = JSON.parse(json).data;
                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');
                    }
                );
            },
            _listProductLabel: function () {
                let param = {
                    params: {
                        page: -1,
                        row: 100,
                        labelCd: '942021080038360001',
                        shopId: $that.productDetailInfo.product.shopId,
                    }
                }
                vc.http.apiGet(
                    'product.queryProductLabel',
                    param,
                    function (json, res) {
                        $that.productLabelList = JSON.parse(json).data;
                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');
                    }
                );
            },
            //增加购物车商品
            addProd: function () {
                $that.goodsNum = parseInt($that.goodsNum) + 1;
                // vc.http.apiPost(
                //     'storeCart.updateStoreCart',
                //     JSON.stringify(_prod), {
                //     emulateJSON: true
                // },
                //     function (json, res) {
                //         //vm.menus = vm.refreshMenuActive(JSON.parse(json),0);
                //         let _json = JSON.parse(json);
                //         if (_json.code == 0) {
                //             //关闭model
                //             return;
                //         }
                //         vc.message(_json.msg);
                //     },
                //     function (errInfo, error) {
                //         vc.message(json);
                //
                //     });
            },
            editProdcartNum: function (_prod,_newNum) {
                _prod.cartNum = _newNum;
                const reg = /^[1-9]\d*$|^0$/;
                if(!reg.test(_newNum)){
                    vc.message("请输入有效数字");
                    return;
                }
                if(_newNum == 0){
                    vc.message("请输入大于零的数字");
                    return;
                }

                vc.http.apiPost(
                    'storeCart.saveStoreCart',
                    JSON.stringify(_prod), {
                    emulateJSON: true
                },
                    function (json, res) {
                        //vm.menus = vm.refreshMenuActive(JSON.parse(json),0);
                        let _json = JSON.parse(json);
                        if (_json.code == 0) {
                            //关闭model
                            return;
                        }
                        vc.message(_json.msg);
                    },
                    function (errInfo, error) {
                        vc.message(json);

                    });
            },
            //减少购物车商品
            minusProd: function () {
                if ($that.goodsNum <= 1) {
                    vc.message("不能在减少了，最小数量为1");
                    return;
                }
                $that.goodsNum = $that.goodsNum - 1;
                // vc.http.apiPost(
                //     'storeCart.updateStoreCart',
                //     JSON.stringify(_prod), {
                //     emulateJSON: true
                // },
                //     function (json, res) {
                //         //vm.menus = vm.refreshMenuActive(JSON.parse(json),0);
                //         let _json = JSON.parse(json);
                //         if (_json.code == 0) {
                //             //关闭model
                //             return;
                //         }
                //         vc.message(_json.msg);
                //     },
                //     function (errInfo, error) {
                //         vc.message(json);
                //
                //     });
            },
            _addToCart: function (_product) {
                vc.http.apiPost(
                    'storeCart.saveStoreCart',
                    JSON.stringify({
                        prodName: _product.prodName,
                        productId: _product.productId,
                        valueId: _product.valueId,
                        storeId: _product.storeId,
                        price: _product.price,
                        shopId: _product.shopId,
                        cartNum: $that.goodsNum,
                    }), {
                    emulateJSON: true
                },
                    function (json, res) {
                        let _json = JSON.parse(json);
                        if (_json.code == 0) {
                            vc.toast("加入购物车成功");
                            return;
                        }else if(_json.code == 404){
                            vc.toast(_json.msg);
                        }else{
                            vc.toast(_json.data);
                        }
                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');
                        vc.toast(errInfo);
                    });
            },
            _helpBtn: function () {
                vc.toast("咨询电话：13345513582");
            },
            _buyNow: function (_product) {


                vc.http.apiPost(
                    'storeCart.saveStoreCart',
                    JSON.stringify({
                        prodName: _product.prodName,
                        productId: _product.productId,
                        valueId: _product.valueId,
                        storeId: _product.storeId,
                        price: _product.price,
                        shopId:_product.shopId,
                        cartNum: $that.goodsNum,
                    }), {
                    emulateJSON: true
                },
                    function (json, res) {
                        let _json = JSON.parse(json);
                        if (_json.code == 0) {
                            vc.toast("加入购物车成功");
                            //vc.jumpToPage("/pageLR.html#/pages/plateform/order?productId=" + _product.productId);
                            vc.jumpToPage('/pageLR.html#/pages/plateform/order?productId='+_product.productId+'&shopId='+_product.shopId+'&valueId='+_product.valueId+'&goodsNum=1');
                            return;
                        }else if(_json.code == 404){
                            vc.toast(_json.msg);
                        }else{
                            vc.toast(_json.data);
                        }
                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');
                        vc.toast(errInfo);
                    });
            },
            websiteAccessLog: function () {
                let user = vc.getData('/nav/getUserInfo');
                let param = '';
                if (user) {
                    param = {
                        loginName: user.name,
                        loginId: user.userId,
                        accessUrl: window.location.href,
                        loginState: '1'
                    }
                } else {
                    param = {
                        accessUrl: window.location.href,
                        loginState: '0'
                    }
                }
                vc.http.apiPost('websiteAccessLog.saveWebsiteAccessLog',
                    JSON.stringify(param), {
                        emulateJSON: true
                    },
                    function(json, res) {
                        let _json = JSON.parse(json);
                        if (_json.code == 0) {
                            return;
                        }
                    },
                    function(errInfo, error) {
                        console.log('请求失败处理');
                        vc.toast(errInfo);
                    });
            }
        }
    });

})(window.vc);
